package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JLabel;

import control.BudgetManager;
import control.IncomeManager;

import javax.swing.JScrollBar;
import javax.swing.SwingConstants;
import javax.swing.JToggleButton;

import enums.Frequency;
import java.awt.Font;

//@author A0081236L
public class BudgetSelectionPanel extends BackgroundPanel{

	private JLabel lblDailyBudget;
	private JLabel lblWeeklyBudget;
	private JLabel lblMonthlyBudget;
	private JLabel lblYearlyBudget;
	private JLabel lblDailyAmount;
	private JLabel lblWeeklyAmount;
	private JLabel lblMonthlyAmount;
	private JLabel lblYearlyAmount;
	private JScrollBar scrollDaily;
	private JScrollBar scrollWeekly;
	private JScrollBar scrollMonthly;
	private JScrollBar scrollYearly;
	private JToggleButton tgbDaily;
	private JToggleButton tgbWeekly;
	private JToggleButton tgbMonthly;
	private JToggleButton tgbYearly;
	
	private BudgetManager _budgetMan;
	private IncomeManager _incomeMan;
	private SidePane _sidePane;
	
	public BudgetSelectionPanel(IncomeManager incomeMan, BudgetManager budgetMan, SidePane sidePane){
		setLayout(null);
		_budgetMan = budgetMan;
		_incomeMan = incomeMan;
		_sidePane = sidePane;
		
		tgbDaily = new JToggleButton();
		tgbDaily.setBounds(53, 76, 60, 23);
		add(tgbDaily);
		tgbDaily.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				setDaily();
			}
		});
		tgbWeekly = new JToggleButton();
		tgbWeekly.setBounds(53, 114, 60, 23);
		add(tgbWeekly);
		tgbWeekly.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				setWeekly();
			}
		});
		tgbMonthly = new JToggleButton();
		tgbMonthly.setBounds(53, 154, 60, 23);
		add(tgbMonthly);
		tgbMonthly.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				setMonthly();
			}
			
		});
		
		tgbYearly = new JToggleButton();
		tgbYearly.setBounds(53, 194, 60, 23);
		add(tgbYearly);
		tgbYearly.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				setYearly();
			}
		});
		
		lblDailyBudget = new JLabel("Daily Budget");
		lblDailyBudget.setBounds(125, 79, 100, 14);
		this.add(lblDailyBudget);
		scrollDaily = new JScrollBar();
		scrollDaily.setOrientation(JScrollBar.HORIZONTAL);
		scrollDaily.setBounds(237, 79, 263, 16);
		add(scrollDaily);
		scrollDaily.addAdjustmentListener(new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent evt) {
				adjustDaily(evt);
			}		
		});		
		lblDailyAmount = new JLabel();
		lblDailyAmount.setHorizontalAlignment(SwingConstants.TRAILING);
		lblDailyAmount.setBounds(500, 81, 60, 14);
		this.add(lblDailyAmount);
		
		
		lblWeeklyBudget = new JLabel("Weekly Budget");
		lblWeeklyBudget.setBounds(125, 117, 100, 14);
		this.add(lblWeeklyBudget);
		scrollWeekly = new JScrollBar();
		scrollWeekly.setOrientation(JScrollBar.HORIZONTAL);
		scrollWeekly.setBounds(237, 117, 263, 16);
		add(scrollWeekly);
		scrollWeekly.addAdjustmentListener(new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent evt) {
				adjustWeekly(evt);
			}		
		});		
		lblWeeklyAmount = new JLabel();
		lblWeeklyAmount.setHorizontalAlignment(SwingConstants.TRAILING);
		lblWeeklyAmount.setBounds(500, 119, 60, 14);
		this.add(lblWeeklyAmount);
		
		
		lblMonthlyBudget = new JLabel("Monthly Budget");
		lblMonthlyBudget.setBounds(125, 157, 100, 14);
		this.add(lblMonthlyBudget);
		scrollMonthly = new JScrollBar();
		scrollMonthly.setOrientation(JScrollBar.HORIZONTAL);
		scrollMonthly.setBounds(237, 157, 263, 16);
		add(scrollMonthly);
		scrollMonthly.addAdjustmentListener(new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent evt) {
				adjustMonthly(evt);
			}		
		});		
		lblMonthlyAmount = new JLabel();
		lblMonthlyAmount.setHorizontalAlignment(SwingConstants.TRAILING);
		lblMonthlyAmount.setBounds(500, 159, 60, 14);
		this.add(lblMonthlyAmount);
		
		
		lblYearlyBudget = new JLabel("Yearly Budget");
		lblYearlyBudget.setBounds(125, 197, 100, 14);
		this.add(lblYearlyBudget);
		scrollYearly = new JScrollBar();
		scrollYearly.setOrientation(JScrollBar.HORIZONTAL);
		scrollYearly.setBounds(237, 197, 263, 16);
		add(scrollYearly);
		scrollYearly.addAdjustmentListener(new AdjustmentListener(){
			public void adjustmentValueChanged(AdjustmentEvent evt) {
				adjustYearly(evt);
			}		
		});		
		lblYearlyAmount = new JLabel();
		lblYearlyAmount.setHorizontalAlignment(SwingConstants.TRAILING);
		lblYearlyAmount.setBounds(500, 199, 60, 14);
		this.add(lblYearlyAmount);
		
		JLabel lblBudget = new JLabel("Budget");
		lblBudget.setHorizontalAlignment(SwingConstants.CENTER);
		lblBudget.setFont(new Font("Century Gothic", Font.PLAIN, 20));
		lblBudget.setBounds(53, 31, 118, 23);
		add(lblBudget);
		
		updateAll();
	}
	
	public void adjustDaily(AdjustmentEvent evt)
	{
		int value;
		value = (int) (evt.getValue()*getMaxBudget(Frequency.DAILY)/90);
		lblDailyAmount.setText(Integer.toString(value));	
		_budgetMan.get_dailyBudget().set(value);
		_budgetMan.updateBudget();
		_sidePane.update();
	}
	
	public void adjustWeekly(AdjustmentEvent evt)
	{
		int value;
		value = (int) (evt.getValue()*getMaxBudget(Frequency.WEEKLY)/90);
		lblWeeklyAmount.setText(Integer.toString(value));
		_budgetMan.get_weeklyBudget().set(value);
		_budgetMan.updateBudget();	
		_sidePane.update();	
		
	}
	
	public void adjustMonthly(AdjustmentEvent evt)
	{
		int value;
		value = (int) (evt.getValue()*getMaxBudget(Frequency.MONTHLY)/90);
		lblMonthlyAmount.setText(Integer.toString(value));
		_budgetMan.get_monthlyBudget().set(value);
		_budgetMan.updateBudget();
		_sidePane.update();
		
	}
	
	public void adjustYearly(AdjustmentEvent evt)
	{
		int value;
		value = (int) (evt.getValue()*getMaxBudget(Frequency.YEARLY)/90);
		lblYearlyAmount.setText(Integer.toString(value));
		_budgetMan.get_yearlyBudget().set(value);
		_budgetMan.updateBudget();
		_sidePane.update();
		
	}
	

	//@author A0098891H
	public void updateAll()
	{
		if(_budgetMan.get_dailyBudget().isOn())
		{
			turnOnDailyUI();
		}
		else
		{
			turnOffDailyUI();
		}
		

		if(_budgetMan.get_weeklyBudget().isOn())
		{
			turnOnWeeklyUI();
		}
		else
		{
			turnOffWeeklyUI();
		}
		
		if(_budgetMan.get_monthlyBudget().isOn())
		{
			turnOnMonthlyUI();
		}
		else
		{
			turnOffMonthlyUI();
		}


		if(_budgetMan.get_yearlyBudget().isOn())
		{
			turnOnYearlyUI();
		}
		else
		{
			turnOffYearlyUI();
		}
		_budgetMan.updateBudget();
		_sidePane.update();	
	}
	
	public double getMaxBudget(Frequency freq)
	{
		return _incomeMan.getExpectedIncomeByFreq(freq);
		
	}
	

	public void setDaily()
	{
		if(tgbDaily.isSelected())
		{
			turnOnDailyUI();
			_budgetMan.get_dailyBudget().turnOn();
		}
		else
		{
			turnOffDailyUI();
			_budgetMan.get_dailyBudget().turnOff();
		}
		_sidePane.update();
	}
	
	public void setWeekly()
	{
		if(tgbWeekly.isSelected())
		{
			turnOnWeeklyUI();
			_budgetMan.get_weeklyBudget().turnOn();
		}
		else
		{
			turnOffWeeklyUI();
			_budgetMan.get_weeklyBudget().turnOff();
		}
		_sidePane.update();
	}
	
	public void setMonthly()
	{
		if(tgbMonthly.isSelected())
		{
			turnOnMonthlyUI();
			_budgetMan.get_monthlyBudget().turnOn();
		}
		else
		{
			turnOffMonthlyUI();
			_budgetMan.get_monthlyBudget().turnOff();
		}
		_sidePane.update();
	}
	
	public void setYearly()
	{
		if(tgbYearly.isSelected())
		{
			turnOnYearlyUI();
			_budgetMan.get_yearlyBudget().turnOn();
		}
		else
		{
			turnOffYearlyUI();
			_budgetMan.get_yearlyBudget().turnOff();
		}
		_sidePane.update();
	}
	
	public void turnOnDailyUI()
	{
		tgbDaily.setText("On");
		tgbDaily.setSelected(true);
		scrollDaily.setEnabled(true);
		scrollDaily.setValue((int) (_budgetMan.get_dailyBudget().get_amount()/getMaxBudget(Frequency.DAILY)*90));
		lblDailyAmount.setVisible(true);
	}
	public void turnOnWeeklyUI()
	{
		tgbWeekly.setText("On");
		tgbWeekly.setSelected(true);
		scrollWeekly.setEnabled(true);
		scrollWeekly.setValue((int) (_budgetMan.get_weeklyBudget().get_amount()/getMaxBudget(Frequency.WEEKLY)*90));
		lblWeeklyAmount.setVisible(true);
	}
	public void turnOnMonthlyUI()
	{
		tgbMonthly.setText("On");
		tgbMonthly.setSelected(true);
		scrollMonthly.setEnabled(true);
		scrollMonthly.setValue((int) (_budgetMan.get_monthlyBudget().get_amount()/getMaxBudget(Frequency.MONTHLY)*90));
		lblMonthlyAmount.setVisible(true);
	}
	public void turnOnYearlyUI()
	{
		tgbYearly.setText("On");
		tgbYearly.setSelected(true);
		scrollYearly.setEnabled(true);
		scrollYearly.setValue((int) (_budgetMan.get_yearlyBudget().get_amount()/getMaxBudget(Frequency.YEARLY)*90));
		lblYearlyAmount.setVisible(true);
	}
	
	public void turnOffDailyUI()
	{
		tgbDaily.setText("Off");
		tgbDaily.setSelected(false);
		scrollDaily.setEnabled(false);
		lblDailyAmount.setVisible(false);
		scrollDaily.setValue(0);
	}
	public void turnOffWeeklyUI()
	{
		tgbWeekly.setText("Off");
		tgbWeekly.setSelected(false);
		scrollWeekly.setEnabled(false);
		lblWeeklyAmount.setVisible(false);
		scrollWeekly.setValue(0);
	}
	public void turnOffMonthlyUI()
	{
		tgbMonthly.setText("Off");
		tgbMonthly.setSelected(false);
		scrollMonthly.setEnabled(false);
		lblMonthlyAmount.setVisible(false);
		scrollMonthly.setValue(0);
	}
	public void turnOffYearlyUI()
	{
		tgbYearly.setText("Off");
		tgbYearly.setSelected(false);
		scrollYearly.setEnabled(false);
		lblYearlyAmount.setVisible(false);
		scrollYearly.setValue(0);
	}
}
